package com.erp.print.view;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.print.PageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author moluwangling
 * @version 2024/11/19 10:49
 * @className PrintFrame
 * @description 配置
 **/
public class PrintFrame extends JFrame {
    Container container = null;
    JLabel jl = null;

    public PrintFrame() {
        container = this.getContentPane();
        this.setTitle("打印PDF");//设置标题

        jl = new JLabel("这是一个JFrame窗体");
        //使标签上的文字居中
        jl.setHorizontalAlignment(SwingConstants.CENTER);
        //将标签添加到容器中
        container.add(jl);
        //设置容器的背景颜色
        container.setBackground(Color.white);
        //设置窗体关闭方式
//        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.dispose();
        this.setSize(new Dimension(430, 300));//设置大小

        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new GridLayout(6, 1, 10, 2));
        JPanel panel01 = new JPanel(null);
        JLabel label01 = new JLabel("打印机 : ");
        label01.setBounds(10, 10, 100, 32);
        //调用接口获取
//        JComboBox<String> serviceComboBox = selectPrintService();
//        serviceComboBox.setBounds(100, 10, 270, 30);
//        panel01.add(label01);
//        panel01.add(serviceComboBox);
        //
        String url = "http://localhost:9018/erp/print/PrintService";
        HttpRequest request = HttpUtil.createPost(url);

        HttpResponse response = request.execute();
        Object result = response.body();
        List<String> list = JSONUtil.toList((String) result, String.class);
        JComboBox<String> serviceComboBox = new JComboBox<>();
        for (String item : list) {
            serviceComboBox.addItem(item);
        }
        serviceComboBox.setSelectedItem(list.get(0));
        serviceComboBox.setBounds(100, 10, 270, 30);
        panel01.add(label01);
        panel01.add(serviceComboBox);

        JPanel panel02 = new JPanel(null);
        JLabel label02 = new JLabel("份数 : ");
        label02.setBounds(10, 10, 100, 32);
        JTextField countField = new JTextField(10);
        countField.setBounds(100, 10, 270, 30);
        countField.setText("1");
        panel02.add(label02);
        panel02.add(countField);

        JPanel panel03 = new JPanel(null);
        JLabel label03 = new JLabel("调整页面大小 : ");
        label03.setBounds(10, 10, 95, 32);
        JPanel right03 = new JPanel(new BorderLayout());
        right03.setBounds(88, 8, 330, 30);
        JPanel radioPanel03 = new JPanel();
        JRadioButton fitBtn01 = new JRadioButton("合适");//SCALE_TO_FIT
        fitBtn01.setSelected(true);//默认适合大小
        JRadioButton fitBtn02 = new JRadioButton("实际大小");//ACTUAL_SIZE
        JRadioButton fitBtn03 = new JRadioButton("缩放裁剪");//SHRINK_TO_FIT
        JRadioButton fitBtn04 = new JRadioButton("拉伸适应");//STRETCH_TO_FIT
        ButtonGroup group03 = new ButtonGroup();
        group03.add(fitBtn01);
        group03.add(fitBtn02);
        group03.add(fitBtn03);
        group03.add(fitBtn04);
        panel03.add(label03);
        radioPanel03.add(fitBtn01);
        radioPanel03.add(fitBtn02);
        radioPanel03.add(fitBtn03);
        radioPanel03.add(fitBtn04);
        right03.add(radioPanel03, BorderLayout.WEST);
        panel03.add(right03);

        JPanel panel04 = new JPanel(null);
        JLabel label04 = new JLabel("方向 : ");
        label04.setBounds(10, 10, 95, 32);
        JPanel right04 = new JPanel(new BorderLayout());

        right04.setBounds(88, 8, 330, 30);
        JPanel radioPanel04 = new JPanel();
        JRadioButton orientationBtn01 = new JRadioButton("纵向");//纵向
        orientationBtn01.setSelected(true);//默认纵向
        JRadioButton orientationBtn02 = new JRadioButton("横向");//横向
        ButtonGroup group04 = new ButtonGroup();
        group04.add(orientationBtn01);
        group04.add(orientationBtn02);
        panel04.add(label04);
        radioPanel04.add(orientationBtn01);
        radioPanel04.add(orientationBtn02);
        right04.add(radioPanel04, BorderLayout.WEST);
        panel04.add(right04);

        JPanel panel05 = new JPanel(null);
        JLabel label05 = new JLabel("单双面打印 : ");
        label05.setBounds(10, 10, 95, 32);
        JPanel right05 = new JPanel(new BorderLayout());
        right05.setBounds(88, 8, 330, 30);
        JPanel radioPanel05 = new JPanel();
        JRadioButton sidesBtn01 = new JRadioButton("单面打印");//单面打印
        JRadioButton sidesBtn02 = new JRadioButton("双面打印");//双面打印
        sidesBtn01.setSelected(true);//默认单面打印
        ButtonGroup group05 = new ButtonGroup();
        group05.add(sidesBtn01);
        group05.add(sidesBtn02);
        panel05.add(label05);
        radioPanel05.add(sidesBtn01);
        radioPanel05.add(sidesBtn02);
        right05.add(radioPanel05, BorderLayout.WEST);
        panel05.add(right05);

        JPanel panel06 = new JPanel();
        JButton confirmBtn = new JButton("确认");//确认
        Map<String, Object> dataMap = new HashMap<>();
        confirmBtn.addActionListener(e -> {
            //获取打印机服务
            String item = serviceComboBox.getSelectedItem().toString();
            dataMap.put("defaultPrintService", item);

            //缩放形式
            if (fitBtn01.isSelected()) {
                //合适大小
                dataMap.put("scaling", "1");
            } else if (fitBtn02.isSelected()) {
                dataMap.put("scaling", "2");
            } else if (fitBtn03.isSelected()) {
                dataMap.put("scaling", "3");
            } else if (fitBtn04.isSelected()) {
                dataMap.put("scaling", "4");
            }
            //页面方向
            if (orientationBtn01.isSelected()) {
                //竖向
                dataMap.put("orientation", "1");
            } else if (orientationBtn02.isSelected()) {
                //横向
                dataMap.put("orientation", "2");
            }
            //单双面打印
            if (sidesBtn01.isSelected()) {
                //单面打印
                dataMap.put("sides", "1");
            } else if (sidesBtn02.isSelected()) {
                //双面打印
                dataMap.put("sides", "2");
            }
            String postUrl = "http://localhost:9018/erp/print/config";
            HttpRequest request1 = HttpUtil.createPost(postUrl).body(String.valueOf(JSONUtil.parse(dataMap)));
            HttpResponse response1 = request1.execute();
            this.dispose();
        });
        JButton cancelBtn = new JButton("取消");//取消
        cancelBtn.addActionListener(e -> {
            this.dispose();//点取消关闭窗口
        });

        panel06.add(confirmBtn);
        panel06.add(cancelBtn);

        contentPanel.add(panel01);
        contentPanel.add(panel02);
        contentPanel.add(panel03);
        contentPanel.add(panel04);
        contentPanel.add(panel05);
        contentPanel.add(panel06);
        this.add(contentPanel);

        this.setLocationRelativeTo(null);//设置屏幕居中
//        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);//设置可见
        this.setResizable(false);//设置大小不可改变
        init();
    }

    public void init() {
        //使窗体可视
        this.setVisible(true);
    }
}
